package com.neu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.neu.mapper.StudentMapper;
import com.neu.pojo.Clazz;
import com.neu.pojo.PageResult;
import com.neu.pojo.Student;
import com.neu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<Student> page(Integer page, Integer pageSize, String name, Integer degree, Integer clazzId) {
        //1.设置分页参数
        PageHelper.startPage(page, pageSize);
        //2.执行查询
        List<Student> studentList = studentMapper.page(name,degree,clazzId);
        //3.解析查询结果，并封装
        Page<Student> p = (Page<Student>) studentList;

        return new PageResult<Student>(p.getTotal(),p.getResult());
    }

    @Override
    public void save(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.save(student);
    }

    @Override
    public void deleteById(Integer id) {
        studentMapper.deleteById(id);
    }

    @Override
    public Student getById(Integer id) {
        Student student = studentMapper.getById(id);
        return student;
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void updateViolation(Integer id, Short score) {
        studentMapper.updateViolate(id,score);
    }


}
